package com.ysd.ziyuanshiyongtongji.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ysd.ziyuanshiyongtongji.dto.MemberDTO;
import com.ysd.ziyuanshiyongtongji.model.Member;
import com.ysd.ziyuanshiyongtongji.service.IMemberService;
import com.ysd.ziyuanshiyongtongji.util.POIUtil;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * MemberController
 */
@ApiOperation("会员列表页")
@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private IMemberService memberService;

    @ApiOperation(value = "分页查询",notes = "分页查询")
    @PostMapping("/findPage")
    public Map<String,Object> findPage(@RequestBody MemberDTO dto){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Member> page = memberService.findPage(dto);
        map.put("code", 0);
        map.put("data", page.getContent());
        return map;
    }

    @ApiOperation(value = "保存会员信息",notes = "添加或修改")
    @PostMapping
    public Member saveMember(@RequestBody Member m){
        return memberService.saveMember(m);
    }

    @ApiOperation(value = "批量添加",notes = "该功能还未实现")
    @PostMapping(value = "/batch")
    public int batchAdd(String path){
        //path参数是前台页面上传时的文件名，这里使用本地文件模拟下
        path = "C:\\Users\\59262\\Desktop\\会员登记表.xlsx";
        List<Member> list = new ArrayList<Member>();
        try {
            list = POIUtil.importMember(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return memberService.addAll(list);
    }

    @ApiOperation(value = "删除会员",notes = "删除会员")
    @ApiImplicitParam(name = "id",value = "主键",required = true,paramType = "path")
    @DeleteMapping("/{id}")
    public void del(@PathVariable Integer id){
        memberService.del(id);
    }

    @ApiOperation(value = "批量删除会员",notes = "批量删除会员")
    @PostMapping("/delAll")
    public void delAll(@RequestBody List<Member> list){
        memberService.delAll(list);
    }


}